package com.tl.sun.ui.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tl.sun.R;
import com.tl.sun.model.ChildMsg;
import com.tl.sun.model.Level0Item;

import java.util.List;


public class SystemMsgAdapter2 extends BaseExpandableListAdapter {
    public Context context;
    public GroupViewHolder gvh;
    public ItemViewHolder ivh;
    public List<Level0Item> groupListData;
    public List<List<ChildMsg>> itemListData;
    private Handler mHandler;

    public SystemMsgAdapter2(Context context, List<Level0Item> groupData, List<List<ChildMsg>> itemListData) {
        this.context = context;
        this.groupListData = groupData;
        this.itemListData = itemListData;
    }

    public void setData(List<Level0Item> groupListData) {
        this.groupListData = groupListData;
        notifyDataSetChanged();
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_expandable_lv0, null);
            gvh = new GroupViewHolder(convertView);
            convertView.setTag(gvh);
        } else {
            gvh = (GroupViewHolder) convertView.getTag();
        }

        gvh.tvTitle.setText(groupListData.get(groupPosition).getTitle());
        gvh.tvDate.setText(groupListData.get(groupPosition).getSend_date());
        gvh.ivIndex.setImageResource(isExpanded ? R.drawable.line_up : R.drawable.line_down);

        if (groupPosition == 0){
            gvh.rlBg.setVisibility(View.VISIBLE);
            String title = groupListData.get(groupPosition).getTitle();
            String substring = title.substring(0, 1);
            gvh.tvLogo.setText(substring);
        }else{
            gvh.rlBg.setVisibility(View.GONE);
        }

        boolean read = groupListData.get(groupPosition).getReadFlag().equals("0");
        if (read) {
            gvh.tvTitle.setTextColor(Color.parseColor("#000000"));
            gvh.tvDate.setTextColor(Color.parseColor("#000000"));
        } else {
            gvh.tvTitle.setTextColor(Color.parseColor("#c6c6c6"));
            gvh.tvDate.setTextColor(Color.parseColor("#c6c6c6"));
        }
        return convertView;
    }

    class GroupViewHolder {
        TextView tvTitle;
        TextView tvDate;
        TextView tvLogo;
        ImageView ivIndex;
        RelativeLayout rlBg;

        public GroupViewHolder(View convertView) {
            tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            tvDate = (TextView) convertView.findViewById(R.id.tv_date);
            tvLogo = (TextView) convertView.findViewById(R.id.tv_msg_bg);
            ivIndex = (ImageView) convertView.findViewById(R.id.iv_index);
            rlBg = (RelativeLayout) convertView.findViewById(R.id.rl_bg);
        }
    }

    @Override
    public int getGroupCount() {
        return groupListData.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupListData.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_expandable_lv1, null);
            ivh = new ItemViewHolder(convertView);
            convertView.setTag(ivh);
        } else {
            ivh = (ItemViewHolder) convertView.getTag();
        }
        ivh.itemTitle.setText(itemListData.get(groupPosition).get(childPosition).getTitle());
        ivh.itemDesc.setText(itemListData.get(groupPosition).get(childPosition).getContent());
        boolean read = groupListData.get(groupPosition).getReadFlag().equals("0");
        if (read) {
            ivh.itemTitle.setTextColor(Color.parseColor("#000000"));
            ivh.itemDesc.setTextColor(Color.parseColor("#000000"));
        } else {
            ivh.itemTitle.setTextColor(Color.parseColor("#c6c6c6"));
            ivh.itemDesc.setTextColor(Color.parseColor("#c6c6c6"));
        }
        return convertView;
    }

    class ItemViewHolder {
        TextView itemTitle;
        TextView itemDesc;

        public ItemViewHolder(View convertView) {
            itemTitle = (TextView) convertView.findViewById(R.id.tv_child_title);
            itemDesc = (TextView) convertView.findViewById(R.id.tv_child_desc);
        }
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return itemListData.get(groupPosition).size();//先根据groupPosition获取某个group项，再获取该group的size
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {//获取某个group项的item
        return itemListData.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        super.registerDataSetObserver(observer);
    }
}
